<?php
// Where the file is going to be placed 
$target_path = "uploads/";

/* Add the original filename to our target path.  
Result is "uploads/filename.extension" */
$target_path = $target_path . date("Y-m-d H:i:s") . " - " . $_POST['email'] . " - " . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
$x_max = 0;
$y_max = 0;
$z_max = 0;
$x_min = 0;
$y_min = 0;
$z_min = 0;
$filepath = $target_path;
$fp = fopen($filepath, "rb");
$section = @file_get_contents($filepath, NULL, NULL, 0, 79);
fseek($fp, 80);
$data = fread($fp, 4);
$numOfFacets = unpack("I", $data);
for ($i = 0; $i < $numOfFacets[1]; $i++){
	//Start Normal Vector
	$data = fread($fp, 4);
	$hold = unpack("f", $data);
	$normalVectorsX[$i] = $hold[1];
	$data = fread($fp, 4);
	$hold = unpack("f", $data);
	$normalVectorsY[$i] = $hold[1];
	$data = fread($fp, 4);
	$hold = unpack("f", $data);
	$normalVectorsZ[$i] = $hold[1];
	//End Normal Vector
	//Start Vertex1
	$data = fread($fp, 4);
	$hold = unpack("f", $data);
	$vertex1X[$i] = $hold[1];
	$data = fread($fp, 4);
	$hold = unpack("f", $data);
	$vertex1Y[$i] = $hold[1];
	$data = fread($fp, 4);
	$hold = unpack("f", $data);
	$vertex1Z[$i] = $hold[1];
	//End Vertex1
	//Start Vertex2
	$data = fread($fp, 4);
	$hold = unpack("f", $data);
	$vertex2X[$i] = $hold[1];
	$data = fread($fp, 4);
	$hold = unpack("f", $data);
	$vertex2Y[$i] = $hold[1];
	$data = fread($fp, 4);
	$hold = unpack("f", $data);
	$vertex2Z[$i] = $hold[1];
	//End Vertex2
	//Start Vertex3
	$data = fread($fp, 4);
	$hold = unpack("f", $data);
	$vertex3X[$i] = $hold[1];
	$data = fread($fp, 4);
	$hold = unpack("f", $data);
	$vertex3Y[$i] = $hold[1];
	$data = fread($fp, 4);
	$hold = unpack("f", $data);
	$vertex3Z[$i] = $hold[1];
	//End Vertex3
	//Attribute Byte Count
	$data = fread($fp, 2);
	$hold = unpack("S", $data);
	$abc[$i] = $hold[1];
	
	$x_vals = array($vertex1X[$i], $vertex2X[$i], $vertex3X[$i]);
	$y_vals = array($vertex1Y[$i], $vertex2Y[$i], $vertex3Y[$i]);
	$z_vals = array($vertex1Z[$i], $vertex2Z[$i], $vertex3Z[$i]);
	if(max($x_vals) > $x_max) {
		$x_max = max($x_vals);
	}
	if(max($y_vals) > $y_max) {
		$y_max = max($y_vals);
	}	
	if(max($z_vals) > $z_max) {
		$z_max = max($z_vals);
	}	
	if(min($x_vals) < $x_min) {
		$x_min = min($x_vals);
	}
	if(min($y_vals) < $y_min) {
		$y_min = min($y_vals);
	}	
	if(min($z_vals) < $z_min) {
		$z_min = min($z_vals);
	}	
	
}
$x_dim = $x_max - $x_min;
$y_dim = $y_max - $y_min;
$z_dim = $z_max - $z_min;

$volume = $x_dim*$y_dim*$z_dim;

$a = ($volume*0.0001) + ($z_dim*0.7);

if(((1-(0.1*log10($z_dim/10)) )/1.099) > 0.96)
{
	$b = 0.96;
}
else
{
	$b = ((1 - (0.1*log10($z_dim/10)))/1.1);
}
$precio = ($a / (1-$b));

$mensaje = "Precio estimado de la pieza: AR$ ".number_format($precio, 0, '.', ',');
} 

else{
    $mensaje = "No se ha podido cotizar. Vuelva al paso anterior e intente nuevamente";
}

?>
<!DOCTYPE html>
<html>
<head>
	<title>Cotizador Online</title>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
	<link rel="Stylesheet" type="text/css" href="css/estilos.css">
	
	
	<script type="text/javascript">

	  var _gaq = _gaq || [];
	  _gaq.push(['_setAccount', 'UA-38446831-1']);
	  _gaq.push(['_trackPageview']);

	  (function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	  })();

	</script>
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="950" height="717" border="0" cellpadding="0" cellspacing="0" align="center" style="margin-top:10px; margin-bottom:20px">
	<tr>
		<td id="header" colspan="3" style="background-image:url('images/web_empresa_01.jpg')" width="950" height="276" alt=""></td>
	</tr>
	<tr>
		<td id="buttons">
			<div>
				<ul id="nav">
					<li id="botonEmpresa"><a href="empresa.html"></a></li>
					<li id="botonContacto"><a href="contacto.php"></a></li>
					<li id="botonCotizador"><a href="cotizador.html"></a></li>
					<li id="botonFotos"><a href="fotos.html"></a></li>
				</ul>
			</div>
		</td>
		<td style="background-image:url('images/web_empresa_03.jpg')" width="590" height="441" alt="" valign="top">
			<div id="mensaje" style="width:70%; height:100px; text-align:center; background-color:white; margin-left:auto; margin-right:auto; padding-top:10px">
				<p><?php echo $mensaje?></h1>
			</div>
		</td>
		<td rowspan="4" style="background-image:url('images/web_empresa_04.jpg')" width="89" height="441" alt=""></td>
	</tr>
</table>
</body>
</html>